package com.qq2008.game.mojie.jdo;

import com.qq2008.game.mojie.server.DeadNpcServer;
import com.qq2008.game.mojie.server.SceneServer;
import com.qq2008.game.mojie.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/***
 * 战斗
 */
public class Battle implements Runnable {
    // 所在地图
    private Room room = null;
    // 攻击者
    private Player attacker = null;
    // 防御者
    private Player defender = null;
    // 战斗消息
    private List<String> battleList = null;
    // 是否结束
    private boolean over = false;
    // 战斗类型
    private BattleType battleType;

    public Battle() {
    }

    public Battle(Room room, BattleType battleType, Player attacker, Player defender) {
        this.room = room;
        this.battleType = battleType;
        this.attacker = attacker;
        this.defender = defender;
        this.battleList = new ArrayList<>();
        this.battleList.add(attacker.getName() + "对" + defender.getName() + "发起了攻击.");
    }

    /***
     * 伤害值计算
     * 双方自身攻击/防御
     * 双方装备攻击/防御
     * 法宝
     * 天气/地形
     * buff
     */
    private int calcBattleHit(int attack, int defence) {
        // 攻击者等级
        int attackerLevel = 1;
        // 攻击者附加攻击力
        int attackerAddAttack = 0;
        // 攻击者技能等级
        int attackerSkillLevel = 1;
        // 攻击者技能系数
        int attackerSkillMod = 1;
        // 攻击者是否狂暴
        boolean attackerIsCritical = CommonUtil.randomInt(0, 1) == 0;
        // 防御者等级
        int defenerLevel = 1;
        // 防御者附加防御力
        int defenerAddDefence = 0;
        // 伤害系数
        float hitXishu = CommonUtil.randomInt(8, 12) * 0.1f;

        // 技能伤害
        int attackerSkillHit = attackerSkillLevel * 8 + (attackerSkillMod * (attackerSkillLevel + 18 / 6));
        // 累加伤害
        attack = attackerLevel * 10 + attack * 8 + attackerSkillHit + attackerAddAttack;
        // 狂暴伤害
        attack = (int) (attackerIsCritical ? attack * 1.4 : attack);
        defence = defenerLevel * 8 + defence * 3 + defenerAddDefence;
        //        System.out.println("=============================");
        //        System.out.println(attack);
        //        System.out.println(defence);
        //        System.out.println("=============================");
        int hit = Math.round((attack - defence) * hitXishu);
        return hit < 0 ? 1 : hit;
    }

    /***
     * 出手
     */
    private boolean chushou() {

        //
        if (null == this.attacker || null == this.defender || this.over) {
            return false;
        }

        // 先手伤害计算和修正
        int attackerHit = calcBattleHit(this.attacker.getMinAttack(), this.attacker.getMinAttack());
        int newDefenerHp = Math.max(this.defender.getHp() - attackerHit, 0);
        this.defender.setHp(newDefenerHp);
        this.battleList.add(this.attacker.getName() + "抡起拳头直奔" + this.defender.getName() + "打去， 产生了" + attackerHit + "点伤害");
        //
        if (newDefenerHp == 0) {
            done();
            return false;
        }
        //
        if (this.defender.getHp() < this.defender.getMaxHP() / 10) {
            this.battleList.add(this.defender.getName() + "感觉到死亡来临!");
        } else if (defender.getHp() < this.defender.getMaxHP() / 5) {
            this.battleList.add(this.defender.getName() + "开始颤抖!");
        }
        this.defender.setHp(newDefenerHp);

        // 后手伤害计算和修正
        int defenerHit = calcBattleHit(this.defender.getMinAttack(), this.attacker.getMinDefence());
        int newAttackerHp = Math.max(this.attacker.getHp() - defenerHit, 0);
        this.attacker.setHp(newAttackerHp);
        this.battleList.add(this.defender.getName() + "对" + this.attacker.getName() + "造成了" + defenerHit + "点伤害");
        //
        if (newAttackerHp == 0) {
            this.done();
            return false;
        }
        //
        if (this.attacker.getHp() < this.attacker.getMaxHP() / 10) {
            this.battleList.add(this.attacker.getName() + "感觉到死亡来临!");
        } else if (this.attacker.getHp() < this.attacker.getMaxHP() / 5) {
            this.battleList.add(this.attacker.getName() + "开始颤抖!");
        }
        this.attacker.setHp(newAttackerHp);
        return true;


    }

    /***
     * 战斗结束
     */
    private void done() {
        System.out.println(this.attacker.hp);
        System.out.println(this.defender.hp);
        // 战斗结束
        this.over = true;
        if (this.attacker.hp == 0) {
            // 战斗信息
            this.battleList.add(this.defender.getName() + "杀死了" + attacker.getName() + "!");
            // 死亡处理
            this.attacker.setHp(0);
            this.attacker.setDead(true);
            // 房间为null的是召唤型怪物
            if (this.attacker instanceof Monster && 0 != this.attacker.getRoomId()) {
                Room room = SceneServer.getRoom(this.room.getId());
                room.getNpcMap().remove(this.attacker.getId());
                Monster npc = (Monster) this.attacker;
                npc.setReliveTime(CommonUtil.getTimestamp() + npc.getRefreshTime());
                DeadNpcServer.saveDeadNpc(npc);
            }

            // 战斗奖励
            if (this.defender instanceof Role && this.battleType == BattleType.PVE) {
                Monster monster = (Monster) this.attacker;
                float xishu = CommonUtil.randomInt(7, 13) * 0.1f;
                int dropExp = Math.round(monster.getDropExp() * xishu);
                int dropMonry = Math.round(monster.getDropMoney() * xishu);
                ((Role) this.defender).addExp(dropExp);
                this.battleList.add("你获得了" + dropExp + "经验");
                this.battleList.add("你获得了" + dropMonry + "金币");
            }
        } else {
            this.battleList.add(this.attacker.getName() + "杀死了" + this.defender.getName() + "!");
            this.defender.setDead(true);
            this.defender.setHp(0);
            if (this.defender instanceof Monster && 0 != this.attacker.getRoomId()) {
                Room room = SceneServer.getRoom(this.room.getId());
                room.getNpcMap().remove(this.defender.getId());
                Monster npc = (Monster) this.defender;
                npc.setReliveTime(CommonUtil.getTimestamp() + npc.getRefreshTime());
                DeadNpcServer.saveDeadNpc(npc);
            }
            // 战斗奖励
            if (this.attacker instanceof Role && this.battleType == BattleType.PVE) {
                Monster monster = (Monster) this.defender;
                float xishu = CommonUtil.randomInt(7, 13) * 0.1f;
                int dropExp = Math.round(monster.getDropExp() * xishu);
                int dropMonry = Math.round(monster.getDropMoney() * xishu);
                ((Role) this.attacker).addExp(dropExp);
                this.battleList.add("你获得了" + dropExp + "经验");
                this.battleList.add("你获得了" + dropMonry + "金币");
            }
        }
    }

    public void run() {
        while (!this.over) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!chushou()) {
                break;
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        //        System.out.println("战斗被销毁...");
    }

    public Player getAttacker() {
        return attacker;
    }

    public void setAttacker(Player attacker) {
        this.attacker = attacker;
    }

    public Player getDefender() {
        return defender;
    }

    public void setDefender(Player defender) {
        this.defender = defender;
    }

    public List<String> getBattleList() {
        if (battleList.size() == 0) {
            List<String> list = new ArrayList<>();
            list.add("你目不转睛的盯着对手");
            return list;
        } else {
            List<String> TempMsg = battleList;
            battleList = new ArrayList<>();
            return TempMsg;
        }
    }

    public void setBattleList(List<String> battleList) {
        this.battleList = battleList;
    }

    public boolean isOver() {
        return over;
    }

    public void setOver(boolean over) {
        this.over = over;
    }
}
